<?php
/**
 * 维修记录
 * @Author    紫梦
 * @DateTime  2018-01-10
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use think\Exception;
use \think\Request;
use \think\Db;
use app\common\model\Repair as RepairModel;
use app\common\model\Cars;
use app\common\request\Oss;

class Repair extends Base
{
    /**
     * 维修记录管理
     */
    public function index()
    {
        $model = RepairModel::All();
        $this->assign('data',$model);
        return $this->fetch();
    }

    /**
     * 删除维修记录
     */
    public function delete($id)
    {
        try {
            RepairModel::destroy($id);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }
    /**
     * 取回维修车辆
     */
    public function revert($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
           
            $model = new RepairModel();
            
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }

            $msg['msg'] = '更新成功';
            return $msg;
        }else{
            $model = RepairModel::get($id);          
            $this->assign('data',$model);
            return $this->fetch();
        }
    }

    /**
     * 编辑维修记录
     */
    public function edit($id)
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            $model = new RepairModel();
            try {
                $model->allowField(true)->isUpdate(true)->save($data);
            } catch (\Exception $e) {
                $msg['status'] = 1;
                $msg['msg'] = '数据未更新';
                return $msg;
            }
            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('repair_file')->insert(['repair_id'=>$data['id'],'file_url'=>$url]);
                    }
                }

            }
            $msg['msg'] = '更新成功';
            return $msg;

        }else{

            $model = RepairModel::get($id);
            $this->assign('data',$model);
            return $this->fetch();

        }
    }

    /**
     * 添加维修记录
     */
    public function add()
    {
        $request = Request::instance();

        if($request->isAjax()){
            $msg['status'] = 0;
            $data = $request->post();
            $car = Cars::get(['car_sn'=>$data['car_sn']]);
            if(!$car){
                $msg['status'] = 1;
                $msg['msg'] = '车牌号不存在,请查正后再添加';
                return $msg;

            }
            $data['car_id'] = $car['car_id'];
            $data['car_sn'] = $car['car_sn'];
            $data['car_unique_id'] = $car['car_unique_id'];
            $data['repair_status'] = 1;
            $model = new RepairModel();
            
    
            $res = $model->allowField(true)->save($data);

            $files = $request->file('uploadfile');
            if($files){
                $oss = new Oss();
                foreach ($files as $key => $value) {
                    $url = $oss->uploadOss($value);                    
                    if($url){
                        Db::name('repair_file')->insert(['repair_id'=>$model->id,'file_url'=>$url]);
                    }
                }

            }
            if($res){

                $msg['msg'] = '添加成功';
                
            }else{

                $msg['status'] = 1;
                $msg['msg'] = '添加失败,请稍后再试';
            }

            return $msg;
        }else{
            

            return $this->fetch();

        }
    }

    /**
     * 删除附件
     */
    public function del_file($id)
    {
        
        $url = Db::name('repair_file')->where(['id'=>$id])->find();
        $oss = new Oss();
        $res = false;
        if($oss->unlinkOss($url['file_url'])){
           
            try {
                Db::name('repair_file')->delete($id);
            } catch (Exception $e) {
                return [
                    'code'=>2,
                    'msg'=>$e->getMessage(),
                    ];
            }
            
        }
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }
}